package com.ethink.warehouse.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ethink.framework.common.db.PageUtils;
import com.ethink.warehouse.api.info.MateInventoryOtherOutLogDetailInfo;
import com.ethink.warehouse.api.request.MateInventoryOtherOutLogDetailAddRequest;
import com.ethink.warehouse.api.request.MateInventoryOtherOutLogDetailQueryRequest;
import com.ethink.warehouse.api.request.MateInventoryOtherOutLogDetailUpdateRequest;
import com.ethink.warehouse.api.response.MateInventoryOtherOutLogDetailAddResponse;
import com.ethink.warehouse.api.response.MateInventoryOtherOutLogDetailDeleteResponse;
import com.ethink.warehouse.api.response.MateInventoryOtherOutLogDetailUpdateResponse;
import com.ethink.warehouse.common.entity.MateInventoryOtherOutLogDetail;
import com.ethink.warehouse.common.mapper.MateInventoryOtherOutLogDetailMapper;
import com.ethink.warehouse.converter.MateInventoryOtherOutLogDetailConverter;
import com.ethink.warehouse.service.MateInventoryOtherOutLogDetailService;

import java.util.ArrayList;
import java.util.List;
import org.springframework.beans.BeanUtils;

/**
 * 其他出库明细 Service接口实现
 *
 * @author default
 * @date 2023-02-08
 */
@Slf4j
@Service
public class MateInventoryOtherOutLogDetailServiceImpl extends ServiceImpl<MateInventoryOtherOutLogDetailMapper, MateInventoryOtherOutLogDetail> implements MateInventoryOtherOutLogDetailService {

    @Autowired
    private MateInventoryOtherOutLogDetailMapper mateInventoryOtherOutLogDetailMapper;

    @Autowired
    private MateInventoryOtherOutLogDetailConverter mateInventoryOtherOutLogDetailConverter;

    @Override
    public Page<MateInventoryOtherOutLogDetailInfo> queryMateInventoryOtherOutLogDetail(MateInventoryOtherOutLogDetailQueryRequest request) {
        return mateInventoryOtherOutLogDetailMapper.queryMateInventoryOtherOutLogDetail(PageUtils.buildPage(request), request);
    }

    @Override
    public MateInventoryOtherOutLogDetailInfo getMateInventoryOtherOutLogDetailById(Long mateInventoryOtherOutLogDetailId) {
        return mateInventoryOtherOutLogDetailMapper.getMateInventoryOtherOutLogDetailById(mateInventoryOtherOutLogDetailId);
    }

    @Override
    public List<MateInventoryOtherOutLogDetailInfo> listMateInventoryOtherOutLogDetail(MateInventoryOtherOutLogDetailQueryRequest request) {
        return mateInventoryOtherOutLogDetailMapper.listMateInventoryOtherOutLogDetail(request);
    }

    @Override
    public MateInventoryOtherOutLogDetailAddResponse addMateInventoryOtherOutLogDetail(MateInventoryOtherOutLogDetailAddRequest request) {
        MateInventoryOtherOutLogDetailAddResponse response = new MateInventoryOtherOutLogDetailAddResponse();
        MateInventoryOtherOutLogDetail mateInventoryOtherOutLogDetail = new MateInventoryOtherOutLogDetail();
        BeanUtils.copyProperties(request, mateInventoryOtherOutLogDetail);
        // FIXME set默认字段
        mateInventoryOtherOutLogDetailMapper.insert(mateInventoryOtherOutLogDetail);
        return response;
    }

    @Override
    public MateInventoryOtherOutLogDetailUpdateResponse updateMateInventoryOtherOutLogDetail(MateInventoryOtherOutLogDetailUpdateRequest request) {
        MateInventoryOtherOutLogDetailUpdateResponse response = new MateInventoryOtherOutLogDetailUpdateResponse();
        LambdaUpdateWrapper<MateInventoryOtherOutLogDetail> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(MateInventoryOtherOutLogDetail::getId, request.getId());
        // FIXME 按实际业务需求set
        updateWrapper.set(MateInventoryOtherOutLogDetail::getOrderNo, request.getOrderNo());
        updateWrapper.set(MateInventoryOtherOutLogDetail::getMateInventoryId, request.getMateInventoryId());
        updateWrapper.set(MateInventoryOtherOutLogDetail::getQty, request.getQty());
        mateInventoryOtherOutLogDetailMapper.update(null, updateWrapper);
        return response;
    }

    @Override
    public MateInventoryOtherOutLogDetailDeleteResponse deleteMateInventoryOtherOutLogDetail(Long mateInventoryOtherOutLogDetailId) {
        MateInventoryOtherOutLogDetailDeleteResponse response = new MateInventoryOtherOutLogDetailDeleteResponse();
        LambdaUpdateWrapper<MateInventoryOtherOutLogDetail> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(MateInventoryOtherOutLogDetail::getId, mateInventoryOtherOutLogDetailId);
        updateWrapper.set(MateInventoryOtherOutLogDetail::getIsDeleted, true);
        mateInventoryOtherOutLogDetailMapper.update(null, updateWrapper);
        return response;
    }

    /**
     * 新增n个
     *
     * @param childrenList
     */
    @Override
    public Boolean addMateInventoryOtherOutLogDetailList(List<MateInventoryOtherOutLogDetail> childrenList) {
        return this.saveBatch(childrenList);
    }

    /**
     * 根据主表的orderNo查询详情表
     *
     * @param orderNo
     * @return
     */
    @Override
    public List<MateInventoryOtherOutLogDetailInfo> getMateInventoryOtherOutLogDetailByOrderNo(String orderNo) {
        return mateInventoryOtherOutLogDetailMapper.queryByOrderNo(orderNo);
    }

    /**
     * 删除详情数据
     *
     * @param orderNo
     * @return
     */
    @Override
    public Boolean deleteDetailByOrderNo(String orderNo) {
        int count = mateInventoryOtherOutLogDetailMapper.update(null,
                Wrappers.<MateInventoryOtherOutLogDetail>lambdaUpdate()
                        .eq(MateInventoryOtherOutLogDetail::getOrderNo, orderNo)
                        .set(MateInventoryOtherOutLogDetail::getIsDeleted, Boolean.TRUE)
        );
        return count > 0;
    }

}
